package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {

    /**
     * 名词解释：
     * 营业额：已完成订单的总金额
     * 有效订单：已完成订单的数量
     * 订单完成率：有效订单数 / 总订单数 * 100%
     * 平均客单价：营业额 / 有效订单数
     * 新增用户：新增用户的数量
     */

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public BusinessDataVO getBusinessData() {
        LocalDateTime begin = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);
        Integer newUsers = userMapper.getCountByMap(map);
        //有效订单数
        Integer useOrder = orderMapper.getOrderCount(begin, end, Orders.COMPLETED);
        //总订单数
        Integer totalOrder = orderMapper.getOrderCount(begin, end,null);
        //订单完成率：有效订单数 / 总订单数 * 100%
        Double orderCompletionRate = 0.0;
        if (totalOrder != 0) {
            orderCompletionRate = useOrder.doubleValue() / totalOrder;
        }
        // 营业额：已完成订单的总金额
        map.put("status",Orders.COMPLETED);
        Double turnover = orderMapper.getSumByMap(map);
        turnover = turnover == null ? 0 : turnover;
        // 平均客单价：营业额 / 有效订单数
        Double unitPrice = 0.0;
        if (useOrder != 0) {
             unitPrice = turnover / useOrder;
        }
        
        Integer validOrderCount = useOrder;

        BusinessDataVO vo = BusinessDataVO.builder()
                .newUsers(newUsers)
                .orderCompletionRate(orderCompletionRate)
                .turnover(turnover)
                .unitPrice(unitPrice)
                .validOrderCount(validOrderCount)
                .build();
        return vo;
    }

    /**
     * POI使用
     * @return
     */
    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        HashMap<Object, Object> map = new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);
        Integer newUsers = userMapper.getCountByMap(map);
        //有效订单数
        Integer useOrder = orderMapper.getOrderCount(begin, end, Orders.COMPLETED);
        //总订单数
        Integer totalOrder = orderMapper.getOrderCount(begin, end,null);
        //订单完成率：有效订单数 / 总订单数 * 100%
        Double orderCompletionRate = 0.0;
        if (totalOrder != 0) {
            orderCompletionRate = useOrder.doubleValue() / totalOrder;
        }
        // 营业额：已完成订单的总金额
        map.put("status",Orders.COMPLETED);
        Double turnover = orderMapper.getSumByMap(map);
        turnover = turnover == null ? 0 : turnover;
        // 平均客单价：营业额 / 有效订单数
        Double unitPrice = 0.0;
        if (useOrder != 0) {
            unitPrice = turnover / useOrder;
        }

        Integer validOrderCount = useOrder;

        BusinessDataVO vo = BusinessDataVO.builder()
                .newUsers(newUsers)
                .orderCompletionRate(orderCompletionRate)
                .turnover(turnover)
                .unitPrice(unitPrice)
                .validOrderCount(validOrderCount)
                .build();
        return vo;
    }

    @Override
    public OrderOverViewVO getOverviewOrders() {
        //全部订单
        Integer allOrders = orderMapper.getAll();
        //已取消数量
        Integer cancelledOrders = orderMapper.getOrderByStatus(Orders.CANCELLED);
        //已完成数量
        Integer completedOrders = orderMapper.getOrderByStatus(Orders.COMPLETED);
        //代派送数量
        Integer deliveredOrders = orderMapper.getOrderByStatus(Orders.CONFIRMED);
        //代接单数量
        Integer waitingOrders = orderMapper.getOrderByStatus(Orders.TO_BE_CONFIRMED);
        return OrderOverViewVO.builder().allOrders(allOrders).cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders).deliveredOrders(deliveredOrders).waitingOrders(waitingOrders).build();
    }

    @Override
    public SetmealOverViewVO getOverviewSetmeals() {
        //起售的套餐数量
        Integer sold = setmealMapper.getStatusCount(StatusConstant.ENABLE);
        //停售的套餐数量
        Integer discontinued = setmealMapper.getStatusCount(StatusConstant.DISABLE);
        return SetmealOverViewVO.builder().sold(sold).discontinued(discontinued).build();
    }

    @Override
    public DishOverViewVO getOverviewDishes() {
        Integer sold = dishMapper.getStatusCount(StatusConstant.ENABLE);
        Integer discontinued = dishMapper.getStatusCount(StatusConstant.DISABLE);
        return DishOverViewVO.builder().sold(sold).discontinued(discontinued).build();
    }
}


























